package com.qf.java2107.yb.hotel.controller.table;

import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.constant.MethodConstant;
import com.qf.java2107.yb.hotel.controller.BaseServlet;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.pojo.Table;
import com.qf.java2107.yb.hotel.service.TableService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author YB
 * @date 2021/12/16
 * @VERSION V1.0
 **/
@WebServlet("/table")
public class TableController extends BaseServlet {


        private TableService tableService = (TableService) BeanFactory.getBean("tableService");

        public String list(HttpServletRequest request, HttpServletResponse response){
            String keyword = request.getParameter("keyword");
            ResultVO vo = tableService.findLikeName(keyword);

            request.setAttribute("keyword",keyword);
            request.setAttribute("tables",vo.getData());
            if(vo.getSuccess()){
                return MethodConstant.FORWARD_PREFIX+getServletContext().getAttribute("backendDetailFolder")+"/table/table-list.jsp";
            }
            return vo.getMessage();
        }

        public String save(HttpServletRequest request, HttpServletResponse response){
            String bName = request.getParameter("bName");
            Table table = new Table();
            table.setTableName(bName);
            ResultVO vo = tableService.save(table);
            if(vo.getSuccess()){
                return MethodConstant.REDIRECT_PREFIX+request.getContextPath()+"/table?method=list";
            }
            return vo.getMessage();
        }

        public String update(HttpServletRequest request, HttpServletResponse response){
            String tableId = request.getParameter("tableId");
            String tableStatus = request.getParameter("tableStatus");

            Table table = new Table();
            table.setTableId(Integer.valueOf(tableId));
            table.setTableStatus(Integer.valueOf(tableStatus));

            ResultVO vo = tableService.update(table);
            if(vo.getSuccess()){
                return MethodConstant.REDIRECT_PREFIX+request.getContextPath()+"/table?method=list";
            }
            return vo.getMessage();
        }

        public String delete(HttpServletRequest request, HttpServletResponse response){
            String tableId = request.getParameter("tableId");
            ResultVO vo = tableService.delete(Integer.valueOf(tableId));

            if(vo.getSuccess()){
                return MethodConstant.REDIRECT_PREFIX+request.getContextPath()+"/table?method=list";
            }
            return vo.getMessage();
        }
}
